<?php

	/** FPDF Table - Version 1.0.20090717
	 * To be used together with FPDF.
	 * Component that lets you create and print tables on PDF files.
	 * http://www.fpdf.org
	 * http://code.google.com/p/fpdfreport
	 * 
	 * @author Iván Bustamante
	 * @modified 7 July 2009 by Iván Bustamante
	 * @category PDF Creation
	 * @license GPL v3
	 * @version 1.0.20090717
	 *  
	 **/

	class FPDFTable{
		private $rows = array();
		private $x = 10;
		private $y = 10;
		private $width = 100;
		private $cellHeight = 10;
		private $bgColour = array(255,255,255);
		private $fontColour = array(0,0,0);
		private $fontSize = 8;
		private $font = "Arial";
		private $fontStyle = "";
		private $border = 1;
		private $borderColour = array(0,0,0);
		
		public function addRow($row){
			$count = count($this->rows);
			$this->rows[$count] = $row;
		}
		
		public function setXY($x, $y){
			$this->x = $x;
			$this->y = $y;
		}
		
		public function setWidth($width){
			$this->width = $width;
		}
		
		public function setCellHeight($height){
			$this->cellHeight = $height;
		}
		
		public function setFontColour($fontColour){
			$this->fontColour = $fontColour;
		}
		
		public function setFont($font){
			$this->font = $font;
		}
		
		public function setFontSize($fontSize){
			$this->fontSize = $fontSize;
		}
		
		public function setFontStyle($fontStyle){
			$this->fontStyle = $fontStyle;
		}
		
		public function setBGColour($bgColour){
			$this->bgColour = $bgColour;
		}
		
		public function setBorder($border){
			$this->border = $border;
		}
		
		public function setBorderColour($borderColour){
			$this->borderColour = $borderColour;
		}
		
		public function draw($FPDF){
			$FPDF->SetDrawColor($this->borderColour[0],$this->borderColour[1],$this->borderColour[2]);
			foreach($this->rows as $rowNumber=>$row){
				$FPDF->SetXY($this->x, $this->y + $rowNumber*$this->cellHeight);
				$this->drawRow($row, $FPDF);
			}
		}
		
		private function drawRow($row, $FPDF){
			if(isset($row['data']))
				$data = $row['data'];
			else{
				echo "FPDF Tables: One or more rows are empty.";
				return;
			}
			
			if (isset($row['bgColour']))
				$bgColour = $row['bgColour'];
			else
				$bgColour = $this->bgColour;
				
			if (isset($row['font']))
				$font = $row['font'];
			else
				$font = $this->font;
				
			if (isset($row['fontColour']))
				$fontColour = $row['fontColour'];
			else
				$fontColour = $this->fontColour;
				
			if (isset($row['fontSize']))
				$fontSize = $row['fontSize'];
			else
				$fontSize = $this->fontSize;
				
			if (isset($row['fontStyle']))
				$fontStyle = $row['fontStyle'];
			else
				$fontStyle = $this->fontStyle;
				
			if ($this->border >= 0)
				$border = 1;
			else
				$border = 0;
				
			$FPDF->SetFont($font, $fontStyle, $fontSize);
			$FPDF->SetTextColor($fontColour[0],$fontColour[1],$fontColour[2]);
			$FPDF->SetFillColor($bgColour[0],$bgColour[1],$bgColour[2]);
			$cellWidth = $this->width / count($data);
			
			foreach($data as $text){
				$FPDF->Cell($cellWidth, $this->cellHeight, $text, $border, 0, 'C', true);
			}
			
		}		
		
		
	}
?>